<template>
	<view style="height: 100%">
		<view class="container">
			<h2 class="f18 c3 f600 tc db">请求授权</h2>
			<p class="db tc mt5 f16 c9">点击授权获取信息</p>
			<image class="logos-img" mode="widthFix"
				src="https://cdn.xtwaixie.com/uploads/20240705/17201811546687e1a2e76c1.png" />
			<!-- <image class="login-img" src="../../img/logos.png" /> -->

			<view>
				<button class="login-but login-but1" @tap="loginIn" v-if="get_number">一键登录</button>
				<button class="login-but login-but1" v-else :disabled="!is_check" @getphonenumber="getNumbers"
					open-type="getPhoneNumber">一键登录</button>
				<!-- <button class="login-but login-but1"  disabled="{{!is_check}}" bindgetphonenumber="getNumbers" open-type="getPhoneNumber">微信授权一键登录</button> -->
				<button class="login-but" @tap="onAuthorization">暂不授权</button>
			</view>
		</view>

		<view style="padding: 0 60rpx">
			<view class="mt25 df ac">
				<van-checkbox :value="is_check" @change="onChange" icon-size="18px">
					<p class="f14 c28" style="font-size: 26rpx !important;">请阅读并同意</p>
				</van-checkbox>
				<navigator class="f14 c10" url="/pages/about/about?type=12" hover-class="none" open-type="navigate"
					style="line-height: 40rpx;font-size: 26rpx !important;">《用户服务协议》</navigator>
				<text class="f14 c28" style="line-height: 40rpx;font-size: 26rpx !important;">和</text>
				<navigator class="f14 c10" url="/pages/about/about?type=2" hover-class="none" open-type="navigate"
					style="line-height: 40rpx;font-size: 26rpx !important;">《隐私政策》</navigator>
			</view>
		</view>
		<view v-if="showpopup">
			<view class="popup_mask"></view>
			<view class="popup_content">
				<view class="title">用户协议及隐私政策提示</view>
				<view class="explain_text">
					欢迎使用夕腾外协微信小程序!我们非常重视您的个人信息和隐私保护，在您使用“夕腾外协微信小程序”之前，请务必审慎阅读
					<text @tap="clickEvent('12')" style="color: #1050c7;">
						《用户服务协议》</text>和
					<text @tap="clickEvent('2')"
						style="color: #1050c7;">《隐私政策》</text>，并充分理解协议条款内容。我们将严格按照您同意的各项条款使用您的个人信息，以便为您提供更好的服务。
				</view>
				<view @click="confirm()" class="agree">同意</view>
				<view @click="back()" class="disagree">不同意</view>
			</view>
		</view>
	</view>
</template>

<script>
	import http from '../../utils/http';
	export default {
		data() {
			return {
				get_number: false,
				code: '',
				avatar: '',
				nickname: '',
				is_check: false,
				sex: '',
				region: '',
				showpopup: false,
				pages: null
			};
		}
		/**
		 * 生命周期函数--监听页面加载
		 */
		,
		onLoad(options) {
			uni.removeStorageSync('token');
			this.showpopup = true
			let that = this;
			this.pages = options.pages
			uni.login({
				success(res) {
					let code = res.code;
					that.setData({
						code: code
					});
				}
			});
		},
		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady() {},
		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow() {
			// let is_login = wx.getStorageSync("is_login");
			// this.setData({
			//   is_login: is_login
			// })
		},
		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide() {},
		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload() {},
		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh() {},
		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom() {},
		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage() {},
		methods: {
			confirm() {
				this.is_check = true
				this.showpopup = false
			},
			back() {
				this.showpopup = false
			},
			clickEvent(i) {
				uni.navigateTo({
					url: "/pages/about/about?type=" + i
				})
			},
			onAuthorization() {
				uni.redirectTo({
					url: '/pages/home/home'
				});
			},

			// 微信用户名称头像
			loginIn(e) {
				if (!this.is_check) {
					uni.showToast({
						title: '请阅读并勾选用户协议、隐私政策',
						icon: 'none'
					});
					return;
				}
				uni.getUserProfile({
					desc: '用于完善会员资料',
					success: (res) => {
						console.log(res);
						this.setData({
							avatar: res.userInfo.avatarUrl,
							nickname: res.userInfo.nickName,
							get_number: false,
							sex: res.userInfo.gender == 0 ? '' : (res.userInfo
								.gender == 1 ? '女' : '男'),
							region: res.userInfo.province + res.userInfo.city
						});
					}
				});
			},

			//  获取手机号
			getNumbers(e) {
				if (!this.is_check) {
					uni.showToast({
						title: '请阅读并勾选用户协议、隐私政策',
						icon: 'none'
					});
					return;
				}
				let that = this;
				let data = {
					code: this.code,
					nickname: this.nickname,
					avatar: this.avatar,
					iv: e.detail.iv,
					encryptedData: e.detail.encryptedData
				};
				http.request('login/userRegister', data)
					.then(async (res) => {
						// http.request
						uni.showToast({
							title: '登录成功'
						});
						uni.setStorageSync('token', res.data.token.token);
						uni.setStorageSync('is_login', res.code);
						await this.getUserInfo()
						// uni.setStorageSync('userInfo',re)
						setTimeout(() => {
							http.request('demand/getIp', {
								type: 3,
								id: 0
								// id:res.data.relation_id
							}).then((rest) => {})
						}, 0)
						setTimeout(() => {
							if (this.pages === 'setPage') {
								uni.redirectTo({
									url: '/pages/settled/setPage'
								});
								return
							}
							if (this.pages === 'demand') {
								uni.redirectTo({
									url: '/pages/demand/demand'
								});
								return
							}
							uni.navigateBack({
								delta: 1,
								fail(err) {
									uni.reLaunch({
										url: '/pages/my/my'
									})
									console.log(err)
								}
							});
						}, 1200);
					})
					.catch((res) => {
						console.log(res);
						uni.showToast({
							title: res.message,
							icon: 'none'
						});
					});
			},
			getUserInfo() {
				return http.request('user/userInfo').then((res) => {
					console.log(res.data.userInfo, "asdasdasd")
					if (res.data.userInfo.status != 1) {
						uni.showToast({
							title: '该账号已被冻结',
							icon: 'none'
						});
						return
					}
					uni.setStorageSync('userInfo', res.data.userInfo);
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: 'none'
					});
				});
			},
			onChange() {
				this.setData({
					is_check: !this.is_check
				});
			}
		}
	};
</script>
<style lang="scss">
	.home {
		position: fixed;
		width: 50rpx;
		height: 50rpx;
		left: 50rpx;
		top: 50rpx;
	}

	.popup_content {
		overflow: hidden;
		box-sizing: border-box;
		padding: 40rpx 40rpx 30rpx;
		position: fixed;
		top: 50%;
		border-radius: 30rpx;
		transform: translate(-50%, -50%);
		left: 50%;
		min-height: 400rpx;
		background: #ffffff;
		width: 650rpx;
		z-index: 99999999999999;

		.title {
			text-align: center;
			font-size: 32rpx;

			padding: 10rpx 0 20rpx;
		}

		.agree {
			text-align: center;
			line-height: 70rpx;
			font-size: 30rpx;
			color: #FFF;
			font-weight: bold;
			border-radius: 50rpx;
			background: #1050c7;
			margin-bottom: 30rpx;
		}

		.disagree {
			font-size: 28rpx;
			text-align: center;
			color: #1050c7;
		}

		.explain_text {
			font-size: 26rpx;
			padding: 0rpx 0 40rpx 0;
			line-height: 1.8;
			text-indent: 2em;

			.line {
				display: block;
				margin-top: 24rpx;
				line-height: 48rpx;

				.path {
					color: #007aff;
					display: inline-block;
					text-align: center;
				}
			}
		}

		.button1 {
			display: flex;
			padding: 20rpx;
			align-items: center;
			font-size: 34rpx;
			justify-content: space-around;
			border-top: 1rpx solid #f2f2f2;

			view {
				text-align: center;
			}
		}
	}
</style>
<style>
	@import './login.css';

	.popup_mask {
		position: fixed;
		bottom: 0;
		top: 0;
		left: 0;
		right: 0;
		background-color: rgba(0, 0, 0, 0.4);
		transition-property: opacity;
		transition-duration: 0.3s;
		opacity: 1;
		z-index: 98;
	}
</style>